package com.walking.slf4jmdc.controller;

import com.walking.slf4jmdc.util.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author walking
 */
@Slf4j
@RestController
public class MDCTestController {
    @Autowired
    @Qualifier("taskExecutor")
    private ThreadPoolTaskExecutor taskExecutor;

    @GetMapping("testMDCThreadPool")
    public Map<String, Object> useMDCThreadPool() {
        Map<String, Object> successResult = ResultUtil.getSuccessResult();
        log.info("begin....");
        //提交任务
        taskExecutor.execute(() -> {
            log.info("task main......");
            try {
                TimeUnit.MILLISECONDS.sleep(1);
                doSome1();
                //other...
            } catch (InterruptedException e) {
                log.error("task error:{}", e);
            }
        });
        log.info("end....");
        return successResult;
    }
    public void doSome1() throws InterruptedException {
        log.info("this is print......");
        TimeUnit.MILLISECONDS.sleep(5);
        doSome2();
        //other...
    }
    public void doSome2() throws InterruptedException {
        log.info("this is print......");
        TimeUnit.MILLISECONDS.sleep(1);
        //other...
    }

}
